﻿#region

using System;
using System.Diagnostics;
using System.Text;

#endregion

namespace BusinessLogicLayer
{
    public static class ExceptionLogBLL
    {
        /// <summary>
        ///     Store exception in the event viewer
        /// </summary>
        /// <param name="exception">Represent's the exception</param>
        public static void Log(Exception exception)
        {
            StringBuilder sbExceptionMessage = new StringBuilder();
            do
            {
                sbExceptionMessage.Append("Exception Type : " + exception.GetType().Name + Environment.NewLine +
                                          Environment.NewLine);
                sbExceptionMessage.Append("Message : " + exception.Message + Environment.NewLine + Environment.NewLine);
                sbExceptionMessage.Append("Stack Trace : " + exception.Message + Environment.NewLine);
                sbExceptionMessage.Append(exception.StackTrace + Environment.NewLine + Environment.NewLine);
                exception = exception.InnerException;
            } while (exception != null);

            string SourceName = "www.fasttrackcabservice.lk";
            string LogName = "FastTrack";

            if (EventLog.SourceExists(SourceName))
            {
                EventLog elog = new EventLog(LogName)
                {
                    Source = SourceName
                };
                elog.WriteEntry(sbExceptionMessage.ToString(), EventLogEntryType.Error);
            }
        }
    }
}